cap program drop stateevent
program de stateevent
cap frame drop coefplot`1'
frame create coefplot`1'
frame coefplot`1': set obs 18
frame coefplot`1': gen estimate = .
frame coefplot`1': gen se = .
frame coefplot`1': gen eyear = -12 + _n
forval i = 1/18 {
loc b`i' = _b[alpha_`i']
loc se_`i' = _se[alpha_`i']
frame coefplot`1': replace estimate = `b`i'' in `i'
frame coefplot`1': replace se = `se_`i'' in `i'

}
frame coefplot`1': gen ci95hi = estimate + 1.96 * se
frame coefplot`1': gen ci95lo = estimate - 1.96 * se 
frame coefplot`1': gen ci90hi = estimate + 1.64 * se 
frame coefplot`1': gen ci90lo = estimate - 1.64 * se


frame coefplot`1': summarize ci95lo
local min_lo = r(min)
frame coefplot`1': summarize ci95hi
local max_hi = r(max)
local ymax = max(abs(`min_lo'), abs(`max_hi'))

frame coefplot`1': local base = 10
* For small values, use a different base
if `ymax' < 2 {
    frame coefplot`1': local base = 1
} 
else if `ymax' < 10 {
    frame coefplot`1': local base = 5
}

* Round up to the nearest multiple of base
frame coefplot`1': local ymax_rounded = ceil(`ymax'/`base')*`base'

* Determine appropriate intervals based on the range size
if `ymax_rounded' > 100 {
    frame coefplot`1': local yinterval = 50
}
else if `ymax_rounded' > 50 {
    frame coefplot`1': local yinterval = 25
}
else if `ymax_rounded' > 20 {
    frame coefplot`1': local yinterval = 10
}
else if `ymax_rounded' > 10 {
    frame coefplot`1': local yinterval = 5
}
else if `ymax_rounded' > 5 {
    frame coefplot`1': local yinterval = 2
}
else {
    frame coefplot`1': local yinterval = 1
}

loc stars ""
if `5' <= .1 {
loc stars "*"
}
if `5' <= .05 {
loc stars "**"
}
if `5' <= .01 {
loc stars "***"
}

    loc yrange r(-`ymax_rounded' `ymax_rounded')
    loc ylabel ylabel(-`ymax_rounded'(`yinterval')`ymax_rounded')

frame coefplot`1': twoway (rspike ci95lo ci95hi eyear ,  lc(black))  (scatter estimate eyear , mc(black) m(o)), graphregion(color(white) lcolor(white) lwidth(vthick)) ylab(, nogrid) title("", size(medsmall) color(black)) subtitle("Top-Quartile × Post = `3'`stars' [`4']", size(medsmall) color(black))  ytitle("Coefficient Estimate") ysc(titleg(2) `yrange') `ylabel' yline(0, lc(gs6) lp(dash))  xtitle("Year Relative to Fracking's Introduction") ysc(titleg(2)) legend(off)



if `2' == 0 {
	frame drop coefplot`1'
}
end